package com.sport.mapper;

import com.sport.pojo.dto.GameResults;
import com.sport.pojo.dto.UserListDto;
import com.sport.pojo.entity.Room;
import com.sport.pojo.vo.RoomStatusVo;
import com.sport.pojo.vo.RoomVo;
import com.sport.pojo.entity.UandR;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface RoomMapper {

    @Select("select room_id,room_status,max_num,current_count,avatar,user_name,begin_time from room as a join user as b on a.roomhost_id=b.user_id where b.user_football_dan between (select user_football_dan from user where user_id=#{currentAccount})-5 and (select user_football_dan from user where user_id=#{currentAccount})+5 and a.battle_status=1 and room_status!=3;")
    List<RoomVo> findRoom(String currentAccount);
    @Insert("insert into room (room_id,roomhost_id,max_num,create_time,begin_time,battle_status)values (#{roomId},#{roomhostId},#{maxNum},#{createTime},#{beginTime},#{battleStatus})")
    int createRoom(Room room);
    @Select("SELECT room_id,room_status,max_num,current_count,avatar from room as a join user as b on a.roomhost_id = b.user_id where user_id =#{user_id};")
    RoomVo selectRoomById(String userId);
    @Insert("insert into uandr (user_id,room_id,camp,img_id)values (#{userId},#{roomId},#{camp},#{imgId})")
    int userIntoRoom(UandR uandR);
    @Update("update uandr set camp = #{camp} where user_id = #{userId} and room_id = #{roomId}")
    int userChangeCamp(UandR uandR);
    @Select("select user.user_id,user_name,avatar,camp,student_card from uandr join user on uandr.user_id = user.user_id where room_id = #{roomId}")
    List <UserListDto> showRoomNumber(String roomId);

    @Update("update room set current_count = current_count - 1 where room_id = #{roomId}")
    void reduceCount(UandR uandR);

    @Delete("delete from uandr where user_id = #{userId} and room_id = #{roomId}")
    int exitRoom(UandR uandR);
    @Update("update room set current_count = current_count+1 where room_id = #{roomId}")
    int addCurrentCount(String roomId);
    @Update("update room set room_status = 2 where room_id = #{roomId}")
    int trailBeginGame(String roomId);
    @Update("update room set room_status = 3,end_time = #{endTime},outcome = #{outcome}, outcome_num = #{outcomeNum}where room_id = #{roomId}")
    int trailSubmitResults(GameResults gameResults);
    @Select("select count(*) from uandr where room_id = #{roomId} and camp = #{camp}")
    int campNum(UandR uandR);
    @Select("select max_num from room where room_id = #{roomId}")
    int getMaxNum(UandR uandR);

    @Select("select * from room where roomhost_id = #{userId}")
    Room findRoomByRoomHostId(String userId);
    @Delete("delete from uandr where room_id = #{roomId}")
    int deleteUandRByRoomId(String roomId);
    @Delete("delete from room where roomhost_id = #{userId}")
    int deleteRoomByUserId(String userId);

    @Delete("delete from uandr where user_id = #{userId}")
    int kickUser(UandR uandR);

    @Select("select room_id,room_status,max_num,current_count,avatar,user_name,begin_time,place from room as a join user as b on a.roomhost_id=b.user_id where a.battle_status=0 and room_status!=3")
    List<RoomVo> findNormalRoom(String currentId);

    @Select("select count(*) from room where room_id = #{roomId} and current_count= max_num")
    int isFull(UandR uandR);
    @Update("update room set room_status = 1 where room_id = #{roomId}")
    void updateRoomStatus(UandR uandR);
    @Update("update room set room_status = 2,place = #{place},begin_time=#{now} where room_id = #{roomId}")
    int startGame(String roomId, String place, LocalDateTime now);
    @Update("update room set room_status = 3,end_time=#{now} where room_id = #{roomId}")
    int  endGame(String roomId, LocalDateTime now);
    @Select("select room_status,battle_status from room where room_id = #{roomId}")
    RoomStatusVo findRoomStatusByRoomId(String roomId);
    List<RoomVo> searchRoom(String roomHostName,int type);
    @Select("select count(*) from uandr where  room_id = #{roomId} and user_id = #{currentId}")
    int isKickUser(String roomId, String currentId);
}
